/**
 * 通讯录管理初始化
 */
var AddressList = {
    id: "AddressListTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
AddressList.initColumn = function () {
    return [
    	{field: 'selectItem', radio: true,visible:false},
        {title: '部门名称', field: 'deptName', align: 'center', valign: 'middle',sortable: true},
        {title: '姓名', field: 'name', align: 'center', valign: 'middle', sortable: true},
    	{title: '手机号码', field: 'phone', align: 'center', valign: 'middle', sortable: true},
    	{title: '电子邮件', field: 'email', align: 'center', valign: 'middle', sortable: true},
    	{title: '操作', field: 'id', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
    		var btn = "<a id='sbtn_"+index+"' ><span class='hisui-icon icon-copy'>复制</span></a>";

    		var clipboard = new ClipboardJS("#sbtn_"+index,{
    			text: function() {
    				var copy="姓名："+row.name+";"+"部门："+row.deptName+";"+"电话:"+row.phone+";"+"邮箱:"+row.email+";";
    				return copy;
    			}
    		});
    		clipboard.on('success', function(e) {
    			console.info('Action:', e.action);
    		    console.info('Text:', e.text);
    		    console.info('Trigger:', e.trigger);
    			Sys.info("复制成功");
    		});
    	 
    		clipboard.on('error', function(e) {
    			console.log(e);
    		});
    		return btn;
    	}},
    ];
};




/**
 * 检查是否选中
 */
AddressList.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
    	AddressList.seItem = selected[0];
        return true;
    }
};

/**
 * 查询通讯录列表
 */
AddressList.search = function (obj) {
    var queryData = {};
    queryData['deptName'] = $("#deptName").val();
    queryData['name'] = $("#name").val();
    queryData['phone'] = $("#phone").val();
    queryData['email'] = $("#email").val();
    queryData['initials'] = obj.id;
    AddressList.table.refresh({query: queryData});
};

$(function () {
	//首字母查询
    $('#AddressListTableToolbar').append('<a href="#" onclick="AddressList.search(this);" class="hisui-linkbutton" id=>ALL</a>');
    var initials=new Array("A","B","C","D","E","F","G","H","J","K","L","M","N","O","P","Q","R","S","T","W","X","Y","Z");
    for(var i=0;i<initials.length;i++){
    	$('#AddressListTableToolbar').append('<a href="#" onclick="AddressList.search(this);" class="hisui-linkbutton" id='+initials[i]+'>'+initials[i]+'</a>');
    };
	
    var defaultColunms = AddressList.initColumn();
    var table = new BSTable(AddressList.id, Sys.backendPath + "/addresslist/list", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-10);
    AddressList.table = table.init();  
    
    $("#AddressListTableToolbar a").on("click",function(){
    	$("#AddressListTableToolbar a").css("background-color","#40a2de");
    	$(this).css("background-color","#1278b8");
    	$(this).css("color","#FFFFFF");
    })
    $.parser.parse();//添加标签后重新渲染页面
    

});
